let obj = {
    name: "Hydra",
    age: 35
};

// let {age,name} = obj;
// console.log("name =",name);
// console.log("age =",age);

// 定义a1和n1变量，值来源于obj
let {age:a1,name:n1,sex} = obj;
console.log("a1 =",a1); // 35
console.log("n1 =",n1); // Hydra
console.log("sex =",sex); // undefined

// 也可以使用对象的方式解构数组，通过索引获取值
let arr = [1,2,3,4,5];
let {0:c1,2:c2} = arr;
console.log("c1 =",c1); // 1
console.log("c2 =",c2); // 3

let obj2 = {
    name: "宋金宇",
    age: 23,
    friend: obj
}

let {name:n2,age:a2,friend} = obj2;
console.log("n2 =",n2); // 宋金宇
console.log("a2 =",a2); // 23
console.log("friend =",friend); // {name: "Hydra",age: 35}

obj.name = "奉先";
console.log("friend.name = ",friend.name);

// 解构赋值是浅拷贝，拷贝的引用地址。
// 特点，原有对象的属性值改变之后，拷贝的对象的属性值跟着变

let {name: n3} = obj2.friend;
console.log("n3 = ",n3);

let {friend: {name: n4}} = obj2;
console.log("n4 =",n4);

let n5 = obj2.friend.name;